<?php

namespace App\Http\Controllers\Merchant;

use App\Config;
use App\Exports\OrderAnalysisExport;
use App\Http\Response;
use App\Order;
use App\OrderAcm;
use App\WalletConfig;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class OrderAcmController extends Controller
{
    use Response;

    protected $main_currency;

    public function __construct()
    {
        $currency = Config::where('mch_id', auth('merchant')->id())->where('title', 'currency_rate')->first();
        $config_value = $currency['value'];
        $this->main_currency = $config_value['main'];
    }

    public function index(Request $request)
    {
        $start_time = $request->get('start_time');
        $end_time = $request->get('end_time');
        if (!$start_time || !$end_time) {
            return $this->error(503, trans('topic.param_error'));
        } else {

            $data = OrderAcm::whereBetween('date', [$start_time, $end_time])->where('mch_id', auth('merchant')->id())->paginate($request->get('pageSize'), '*', 'pageNo');
            return $this->success(200, ['list' => $data, 'main_currency' => $this->main_currency]);
        }
    }

    public function daily_order()
    {


        $total = Order::whereDate('created_at', date('Y-m-d'))->count();
        $total_yesterday = Order::whereDate('created_at', Carbon::now()->subDay()->format('Y-m-d'))->where('status', 1)->get();
        $total_yesterday_amount = $total_yesterday->sum(function ($list) {

            return $list['pay_amount'];

        });
        $order_list = Order::whereDate('created_at', date('Y-m-d'))->where('status', 1)->get();
        $total_amount = $order_list->sum(function ($list){

            return $list['pay_amount'];

        });
        $unpaid=Order::where('status',0)->count();
        $receive=Order::where('status',1)->where('receivingStatus',1)->count();
        $undelivery=Order::where('status',1)->where('deliveryStatus',0)->count();
        $unreive=Order::where('status',1)->where('receivingStatus',0)->where('deliveryStatus',1)->count();
        $delivery=Order::where('status',1)->where('deliveryStatus',1)->count();
        $result = [
            'total_orders' => $total,
            'yesterday_paid_amount' => $total_yesterday_amount,
            'today_paid_amount' => $total_amount,
            'unpaid'=>$unpaid,
            'receive'=>$receive,
            'undelivery'=>$undelivery,
            'unreceive'=>$unreive,
            'delivery'=>$delivery
        ];

        return $this->success(200, $result);
    }

    public function monthly_order()
    {
        $list = OrderAcm::where([['mch_id', '=', auth('merchant')->id()]])->whereMonth('date', date('Y-m'))->get();
        $total = $list->sum(function ($list) {
            return $list['total_order_number'];
        });
        $paid = $list->sum(function ($list) {
            return $list['paid_order_number'];
        });
        $awaiting = $list->sum(function ($list) {
            return $list['awaiting_order_number'];
        });
        $cancelled = $list->sum(function ($list) {
            return $list['cancelled_orders'];
        });
        $total_amount = $list->sum(function ($list) {
            return $list['total_amount'];
        });

        return $this->success(200, compact('total', 'cancelled', 'paid', 'awaiting', 'total_amount'));
    }

    public function export(Request $request)
    {
        $start_time = $request->get('start_time');
        $end_time = $request->get('end_time');

        return Excel::download(new OrderAnalysisExport($start_time, $end_time, $this->main_currency), time() . "orderAnalysis.xls");
    }

}
